<template>
  <div class="index">
    <zh-card title="">
      <zh-form ref="fileQueryForm" :row-flag="false" :model="state.queryParam" :rules="rules">
        <zh-form-item :span="12" prop="fileClassCode" label="文件分类">
          <zh-select-tree
              :check-strictly="true"
              v-model="state.queryParam.fileClassCode"
              :data="state.fileClassTree"
              ref="classSelect"
              node-key="key"
              :props="{ label: 'title' }"
          >
          </zh-select-tree>
        </zh-form-item>
        <zh-form-select
            :span="12"
            prop="appId"
            :lazy="true"
            label="我的应用"
            v-model="state.queryParam.appId"
            :fieldNames="{ label: 'appName', value: 'appId' }"
            funcCode="application0012"
            searchField="appName"
        ></zh-form-select>
        <zh-form-select
            :span="12"
            prop="upAppId"
            :lazy="true"
            label="所属应用"
            v-model="state.queryParam.upAppId"
            :fieldNames="{ label: 'appName', value: 'appId' }"
            funcCode="application0012"
            searchField="appName"
        ></zh-form-select>
        <zh-form-item :span="12" prop="secrecyLevel" label="保密级别">
          <el-radio-group  v-model="state.queryParam.secrecyLevel" size="large">
            <div>
              <el-radio style="margin-left:20px" label="" >全部</el-radio>
            </div>
            <div v-for="item in state.dictData.secrecyLevel">
              <el-radio style="margin-left:20px"  :label="item.dictKey" >{{item.dictValue}}</el-radio>
            </div>
          </el-radio-group>
        </zh-form-item>
        <zh-form-input  v-if="state.advanced" prop="fileName" label="文件名称"  v-model="state.queryParam.fileName"
        ></zh-form-input>
        <zh-form-select
            v-if="state.advanced"
            prop="fieldId"
            :lazy="true"
            label="元数据"
            v-model="state.queryParam.fieldId"
            :fieldNames="{ label: 'fieldName', value: 'fieldId' }"
            :url="VITE_FILE_API_URL+'fileFieldParam0001'"
            searchField="fieldName"
        ></zh-form-select>
        <zh-form-input v-if="state.advanced" prop="fieldValue" label="文件元数据值"  v-model="state.queryParam.fieldValue"
        ></zh-form-input>
        <zh-form-item :span="24">
            <zh-form-query style="display: inline;margin-left:20px" v-model="state.advanced" :form-ref="fileQueryForm" @queryClick="queryFile"></zh-form-query>
        </zh-form-item>
      </zh-form>
    </zh-card>
    <zh-card :title="'数据接口'+total+'项'">
      <el-scrollbar height="400px">
        <div :key="item.showId" v-for="item in state.fileList" >
          <el-descriptions
              class="margin-top"
              :title="item.fileName"
              :column="4"
          >
            <template #extra>
              <el-button :icon="Link" type="primary"  @click="link(item)">查看元数据</el-button>
              <el-button :icon="DocumentAdd" v-if="item.appReadFlag==='0'" type="primary" @click="apply(item)">立即申请</el-button>
              <el-button :icon="Flag" :disabled="true" v-if="item.appReadFlag==='1'" type="primary" >已授权</el-button>
            </template>
            <el-descriptions-item label="文件显示id">{{ item.showId }}</el-descriptions-item>
            <el-descriptions-item label="文件分类">{{ item.classTitle }}</el-descriptions-item>
            <el-descriptions-item label="文件后缀">{{ item.fileSuffix }}</el-descriptions-item>
            <el-descriptions-item label="文件地址">{{ item.filePath }}</el-descriptions-item>
            <el-descriptions-item label="上传应用">
              <el-tag size="small">{{ item.upAppName }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="上传时间">{{ item.upTime }}</el-descriptions-item>
            <el-descriptions-item label="保密级别">
              <el-select v-model="item.secrecyLevel"  disabled>
                <el-option
                    v-for="item in state.dictData.secrecyLevel"
                    :key="item.dictKey"
                    :label="item.dictValue"
                    :value="item.dictKey"
                />
              </el-select>
            </el-descriptions-item>
            <el-descriptions-item label="是否加密">
              <el-select v-model="item.encryptionFlag"  disabled>
                <el-option
                    v-for="item in state.dictData.flag"
                    :key="item.dictKey"
                    :label="item.dictValue"
                    :value="item.dictKey"
                />
              </el-select>
            </el-descriptions-item>
          </el-descriptions>
          <el-divider />
        </div>
      </el-scrollbar>
      <el-pagination
          v-model:current-page="currentPage4"
          v-model:page-size="pageSize4"
          :page-sizes="[10, 20, 30, 40]"
          :small="small"
          :disabled="disabled"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="queryFile"
          @current-change="queryFile"
          style="margin:10px;float:right"
      />
    </zh-card>
    <zh-dialog
        ref="fileAppReadInfo"
        v-model="state.fileVisible"
        title="申请读取权限"
        width="30%"
        @confirm="saveFileAppRead"
    >
      <file-app-read-info ref="fileAppReadRef"></file-app-read-info>
    </zh-dialog>
    <el-dialog
        ref="fileMetaDataDialog"
        v-model="state.fileMetaDataVisible"
        title="文件元数据"
        width="80%"
    >
      <file-meta-data ref="fileMetaDataRef" disabled="true"></file-meta-data>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="fileHomeIndex">
import FileAppReadInfo from '@/views/fileCenter/fileHome/component/FileAppReadInfo.vue';
import {Link,Flag,DocumentAdd} from '@element-plus/icons-vue'
import FileMetaData from '@/views/fileCenter/fileManager/component/fileMetaData.vue';
const VITE_FILE_API_URL = import.meta.env.VITE_FILE_API_URL as string+'/doc/api/';

//-----------------定义组件--------------

const fileQueryForm = ref<any>({});
const apiTable = ref<any>({});
//分页参数定义
const currentPage4 = ref(0)
const pageSize4 = ref(10)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const total = ref(0)
const fileAppReadRef=ref();
const fileMetaDataRef=ref();

const rules = reactive({
  appId: [
    {
      required: true,
      message: '请选择我的应用',
      trigger: 'blur',
    },
  ],
});
const state = reactive({
  fileMetaDataVisible:false,
  //文件分类树数据
  fileClassTree:[],
  //查询条件
  queryParam: {} as any,
  dictData: {},
  advanced:false,
  fileList:[],
  fileVisible:false
});




//---------------------初始化---------------------
const params = {
  dictFields: 'secrecyLevel,flag',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;
});


//---------------------自定义方法----------------------

//获取文件分类数据
$api.common.fileSend.post('fileClassM0005', {}).then((res) => {
  state.fileClassTree=res.result.treeList;
}).catch(() => {});

//查询文件元数据信息
function link(row){
  let info= {fileId:row.fileId};
  state.fileMetaDataVisible=true;
  nextTick(()=>{
    fileMetaDataRef.value.setInitData(info);
  })
}
//查询文件
function queryFile(){
  const params = state.queryParam;
  params.currentPage=currentPage4.value;
  params.pageNum=currentPage4.value;
  params.pageSize=pageSize4.value;

  $api.common.fileSend.post('fileManager0009', params).then((res) => {
    total.value=parseInt(res.result.total);
    state.fileList=res.result.list;
  }).catch(() => {});
}

//申请文件权限
function apply(item){
  state.fileVisible=true
  nextTick(()=>{
    let info={};
    info.fileClassCode=item.fileClassCode;
    info.classTitle=item.classTitle;
    fileAppReadRef.value.setInitData(info);
  });
}

//保存文件权限
function saveFileAppRead(){
  let fileAppReadInfo = fileAppReadRef.value.fileAppReadInfo;
  fileAppReadInfo.authStatus='0'
  $api.common.fileSend.post('fileAppRead0004',fileAppReadInfo).then((res) => {
    state.fileVisible=false;
    $messageBox.success('申请授权成功', '提示');
  }).catch(() => {
    $messageBox.error('申请授权失败', '提示');
    state.fileVisible=false;
  });
}
</script>
